package com.example.vv.model.setupTreeList;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.ArticleListEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.fragmentWork.view.CustomerTitleBar;
import com.example.vv.model.setupTreeList.base.adapter.STLAdapter;
import com.example.vv.model.setupTreeList.base.contract.STLContract;
import com.example.vv.model.setupTreeList.base.presenter.STLPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class SetupTreeListActivity extends MvpActivity<STLContract.View, STLPresenter> implements STLContract.View {

    @BindView(R.id.stl_title)
    CustomerTitleBar stlTitle;
    @BindView(R.id.stl_recycler)
    RecyclerView stlRecycler;
    @BindView(R.id.stl_smart)
    SmartRefreshLayout stlSmart;
    STLAdapter adapter;
    int page = 0;
    @Override
    public int initLayout() {
        return R.layout.activity_setup_tree_list;
    }

    @Override
    public STLPresenter initPresenter() {
        return new STLPresenter();
    }

    @Override
    public void initView() {
        stlTitle.getLeft_img().setImageResource(R.mipmap.ic_back);
        stlTitle.getLeft_img().setOnClickListener(v->{
            finish();
        });
        stlTitle.getCenter_tx().setText(getIntent().getStringExtra("name"));
        stlSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                presenter.setArticleList("https://www.wanandroid.com/article/list/"+page+"/json?cid="+getIntent().getIntExtra("id",60));
                stlSmart.finishLoadMore(1000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (adapter!=null) {
                    adapter.clear();
                    presenter.setArticleList("https://www.wanandroid.com/article/list/0/json?cid="+getIntent().getIntExtra("id",60));
                    page=0;
                }
                stlSmart.finishRefresh(1000);
            }
        });
        stlSmart.autoRefresh();
    }

    @Override
    public void initData() {
        presenter.setArticleList("https://www.wanandroid.com/article/list/0/json?cid="+getIntent().getIntExtra("id",60));
        adapter = new STLAdapter(this);
        stlRecycler.setAdapter(adapter);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }

    @Override
    public Context getMContext() {
        return getMContext();
    }

    @Override
    public void getArticleList(List<ArticleListEntity.DatasBean> articleListEntity) {
       adapter.addData(articleListEntity);
    }
}